package org.pentaho.di.ui.core.widget;

import java.io.IOException;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.apache.commons.vfs2.FileObject;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.pentaho.di.base.AbstractMeta;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.util.StringUtil;
import org.pentaho.di.core.vfs.KettleVFS;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.repository.Repository;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.ui.core.dialog.ErrorDialog;
import org.pentaho.di.ui.repository.dialog.SelectObjectDialog;
import org.pentaho.di.ui.spoon.Spoon;
import org.pentaho.vfs.ui.VfsFileChooserDialog;

public class VFSFileSelection extends Composite {
    private static final Class<?> PKG = VFSFileSelection.class;
    public final TextVar wFileName;
    public final Button wBrowse;
    private String initialScheme;
    private boolean showLocation;
    private final String[] fileFilters;
    private final String[] fileFilterNames;
    private final AbstractMeta abstractMeta;
    private final Repository repository;
    private final Supplier<Optional<String>> fileNameSupplier;
    private final int fileDialogMode;

    public VFSFileSelection(Composite composite, int i, String[] fileFilters, String[] fileFilterNames, AbstractMeta abstractMeta) {
        this(composite, i, fileFilters, fileFilterNames, abstractMeta, (Repository)null);
    }

    public VFSFileSelection(Composite composite, int i, String[] fileFilters, String[] fileFilterNames, AbstractMeta abstractMeta, Repository repository) {
        this(composite, i, fileFilters, fileFilterNames, abstractMeta, repository, "file", true);
    }

    public VFSFileSelection(Composite composite, int i, String[] fileFilters, String[] fileFilterNames, AbstractMeta abstractMeta, Repository repository, String initialScheme, boolean showLocation) {
        this(composite, i, fileFilters, fileFilterNames, abstractMeta, repository, initialScheme, showLocation, 0);
    }

    public VFSFileSelection(Composite composite, int i, String[] fileFilters, String[] fileFilterNames, AbstractMeta abstractMeta, Repository repository, String initialScheme, boolean showLocation, int fileDialogMode) {
        super(composite, i);
        this.initialScheme = initialScheme;
        this.showLocation = showLocation;
        this.fileFilters = fileFilters;
        this.fileFilterNames = fileFilterNames;
        this.abstractMeta = abstractMeta;
        this.repository = repository;
        this.fileDialogMode = fileDialogMode;
        this.fileNameSupplier = repository == null ? this::promptForFile : this::promptForRepositoryFile;
        FormLayout formLayout = new FormLayout();
        formLayout.marginWidth = 0;
        formLayout.marginHeight = 0;
        formLayout.marginTop = 0;
        formLayout.marginBottom = 0;
        this.setLayout(formLayout);
        this.wFileName = new TextVar(this.abstractMeta, this, 18436);
        FormData fdFileName = new FormData();
        fdFileName.left = new FormAttachment(0, 0);
        fdFileName.top = new FormAttachment(0, 0);
        fdFileName.width = 275;
        this.wFileName.setLayoutData(fdFileName);
        this.wBrowse = new Button(this, 8);
        this.wBrowse.setText("浏览");
        FormData fdBrowse = new FormData();
        fdBrowse.left = new FormAttachment(this.wFileName, 5);
        fdBrowse.top = new FormAttachment(this.wFileName, 0, 128);
        this.wBrowse.setLayoutData(fdBrowse);
        this.wBrowse.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
                Optional var10000 = (Optional)VFSFileSelection.this.fileNameSupplier.get();
                TextVar var10001 = VFSFileSelection.this.wFileName;
                var10000.ifPresent(var10001::setTextAdd);
            }
        });
    }

    private Optional<String> promptForFile() {
        String curFile = this.abstractMeta.environmentSubstitute(this.wFileName.getText());

        try {
            FileObject root = KettleVFS.getFileObject(curFile != null ? curFile : Const.getUserHomeDirectory());
            VfsFileChooserDialog vfsFileChooser = Spoon.getInstance().getVfsFileChooserDialog(root.getParent(), root);
            if (StringUtil.isEmpty(this.initialScheme)) {
                this.initialScheme = "file";
            }

            FileObject file = vfsFileChooser.open(this.getShell(), (String[])null, this.initialScheme, true, curFile, this.fileFilters, this.fileFilterNames, false, this.fileDialogMode, this.showLocation, true);
            if (file == null) {
                return Optional.empty();
            } else {
                String filePath = this.getRelativePath(file.getName().toString());
                return Optional.ofNullable(filePath);
            }
        } catch (KettleException | IOException var6) {
            new ErrorDialog(this.getShell(), BaseMessages.getString(PKG, "VFSFileSelection.ErrorLoadingFile.DialogTitle", new String[0]), BaseMessages.getString(PKG, "VFSFileSelection.ErrorLoadingFile.DialogMessage", new String[0]), var6);
            return Optional.empty();
        }
    }

    private String getRelativePath(String filePath) {
        String parentFolder = null;

        try {
            parentFolder = KettleVFS.getFileObject(this.abstractMeta.environmentSubstitute(this.abstractMeta.getFilename())).getParent().toString();
        } catch (Exception var4) {
        }

        if (filePath != null && parentFolder != null && filePath.startsWith(parentFolder)) {
            filePath = filePath.replace(parentFolder, "${Internal.Entry.Current.Directory}");
        }

        return filePath;
    }

    private Optional<String> promptForRepositoryFile() {
        SelectObjectDialog sod = new SelectObjectDialog(this.getShell(), this.repository);
        String fileName = sod.open();
        RepositoryDirectoryInterface repdir = sod.getDirectory();
        if (fileName != null && repdir != null) {
            String path = this.getRepositoryRelativePath(repdir + "/" + fileName);
            return Optional.ofNullable(path);
        } else {
            return Optional.empty();
        }
    }

    private String getRepositoryRelativePath(String path) {
        String parentPath = this.abstractMeta.getRepositoryDirectory().getPath();
        if (path.startsWith(parentPath)) {
            path = path.replace(parentPath, "${Internal.Entry.Current.Directory}");
        }

        return path;
    }
}

